/**
 * Created By kane at 2019/2/20 11:15 AM
 */

/**
 * Created By kane at 2019/1/14 10:29 AM
 */

var userCenter = {
        data : {

        },
        start : function () {
            this.onLoad();
            this.bindEvent();
        },
        onLoad : function () {
            this.initPageData();
        },
        bindEvent : function () {
            var _this = this;
            $("#updatePasswd").click(function () {
                _this.updatePasswd();
            });
            $(document).on('mouseover', '#updatePasswd', function () {
                _this.data.tipsIndex = layer.tips('修改密码', '#updatePasswd', {
                    tips: [1, '#FFB800;'] //还可配置颜色
                });
            })
            $(document).on('mouseout', '#updatePasswd', function () {
                layer.close(_this.data.tipsIndex);
            })
        },
        initPageData : function () {
            var loadingIndex = sinoUtil.loading();
            sinoUtil.sendHttp(sinoUtil.httpPrefix + "/api/user/getUserCenterInfo", "POST", {}, function (res) {
                if (res.status == 0) {
                    $("#user_name").text(res.data.userName ? res.data.userName : "--");
                    $("#user_email").text(res.data.userMail ? res.data.userMail : "--");
                    $("#user_phone").text(res.data.userPhone ? res.data.userPhone : "--");
                    if (res.data.isManager == 0) {
                        $("#user_position").text("部门领导");
                    } else if (res.data.isManager == 1) {
                        $("#user_position").text("职员");
                    } else if (res.data.isManager == 2) {
                        $("#user_position").text("管理员");
                    } else {
                        $("#user_position").text("--");
                    }
                } else if (res.status == 10 || res.status == 11) {
                    window.location.href = "/system/login";
                } else {
                    sinoUtil.stopLoadingAndShowResult(loadingIndex, 2, "用户信息查询异常，请联系管理员");
                }
            }, function (error) {
                sinoUtil.stopLoadingAndShowResult(loadingIndex, 2, "发生未知错误，请刷新重试");
            }, false);
            sinoUtil.stopLoadingAndShowResult(loadingIndex, 1);
        },
        updatePasswd : function () {
            layer.open({
                title      : '修改密码',
                type       : 1,
                content    : $('#update_passwd_form_content'),
                area       : ['500px', '300px'],
                btn        : ['提交', '取消'],
                btnAlign   : 'c',
                shadeClose : false,
                maxmin     : true,
                skin       : 'layui-layer-molv',
                success    : function (layero, index) {

                },
                end        : function () {
                    $("#origin_passwd").val("").removeClass("error");
                    $("#new_passwd").val("").removeClass("error");
                    $("#new_passwd_repeat").val("").removeClass("error");
                },
                btn1       : function (index, layero) {
                    var loadingIndex = sinoUtil.loading();
                    if (!sinoUtil.validate($("#origin_passwd").val(), 'require')) {
                        $('#origin_passwd').addClass("error");
                        layer.msg('原密码不能为空', {icon: 2, anim: 6});
                        sinoUtil.stopLoadingAndShowResult(loadingIndex, 2);
                        return false;
                    } else {
                        $('#origin_passwd').removeClass("error");
                    }
                    if (!sinoUtil.validate($("#origin_passwd").val(), 'password')) {
                        $('#origin_passwd').addClass("error");
                        layer.msg('请输入有效密码（大于等于6位）', {icon: 2, anim: 6});
                        sinoUtil.stopLoadingAndShowResult(loadingIndex, 2);
                        return false;
                    } else {
                        $('#origin_passwd').removeClass("error");
                    }
                    if (!sinoUtil.validate($("#new_passwd").val(), 'require')) {
                        $('#new_passwd').addClass("error");
                        layer.msg('新密码不能为空', {icon: 2, anim: 6});
                        sinoUtil.stopLoadingAndShowResult(loadingIndex, 2);
                        return false;
                    } else {
                        $('#new_passwd').removeClass("error");
                    }
                    if (!sinoUtil.validate($("#new_passwd").val(), 'password')) {
                        $('#new_passwd').addClass("error");
                        layer.msg('请输入有效密码（大于等于6位）', {icon: 2, anim: 6});
                        sinoUtil.stopLoadingAndShowResult(loadingIndex, 2);
                        return false;
                    } else {
                        $('#new_passwd').removeClass("error");
                    }
                    if (!sinoUtil.validate($("#new_passwd_repeat").val(), 'require')) {
                        $('#new_passwd_repeat').addClass("error");
                        layer.msg('新密码重复不能为空', {icon: 2, anim: 6});
                        sinoUtil.stopLoadingAndShowResult(loadingIndex, 2);
                        return false;
                    } else {
                        $('#new_passwd_repeat').removeClass("error");
                    }
                    if (!sinoUtil.validate($("#new_passwd_repeat").val(), 'password')) {
                        $('#new_passwd_repeat').addClass("error");
                        layer.msg('请输入有效密码（大于等于6位）', {icon: 2, anim: 6});
                        sinoUtil.stopLoadingAndShowResult(loadingIndex, 2);
                        return false;
                    } else {
                        $('#new_passwd_repeat').removeClass("error");
                    }
                    if ($('#new_passwd').val() !== $('#new_passwd_repeat').val()) {
                        $('#new_passwd_repeat').addClass("error");
                        layer.msg('两次输入密码不一致', {icon: 2, anim: 6});
                        sinoUtil.stopLoadingAndShowResult(loadingIndex, 2);
                        return false;
                    } else {
                        $('#new_passwd_repeat').removeClass("error");
                    }
                    if ($('#origin_passwd').val() === $('#new_passwd_repeat').val()) {
                        $('#new_passwd_repeat').addClass("error");
                        layer.msg('修改后密码与原密码一致', {icon: 2, anim: 6});
                        sinoUtil.stopLoadingAndShowResult(loadingIndex, 2);
                        return false;
                    } else {
                        $('#new_passwd_repeat').removeClass("error");
                    }
                    sinoUtil.sendHttp(sinoUtil.httpPrefix + "/api/user/userCenterUpdatePasswd", "POST", {
                        originPasswd : $("#origin_passwd").val(),
                        newPasswd    : $("#new_passwd_repeat").val()
                    }, function(res){
                        if(res.status == 0){
                            layer.close(index);
                            sinoUtil.stopLoadingAndShowResult(loadingIndex, 1, "密码修改成功，即将自动跳转到登录页", 3000);
                            setTimeout(function f() {
                                var storage = window.localStorage;
                                storage.removeItem("token");
                                window.parent.location.href = "/system/login";
                            }, 3000);
                        }else if(res.status == 10 || res.status == 11){
                            window.parent.location.href = "/system/login";
                        }else{
                            sinoUtil.stopLoadingAndShowResult(loadingIndex, 2, res.msg);
                        }
                    }, function(error){
                        sinoUtil.stopLoadingAndShowResult(loadingIndex, 2, "发生未知错误，请刷新重试");
                    }, false);
                },
                btn2       : function (index, layero) {
                    layer.confirm('系统没有保存您的编辑，确定要取消吗？', function (confirmIndex) {
                        layer.close(confirmIndex);
                        layer.close(index);
                    });
                    return false;
                },
                cancel     : function(index, layero){
                    layer.confirm('系统没有保存您的编辑，确定要取消吗？', function (confirmIndex) {
                        layer.close(confirmIndex);
                        layer.close(index);
                    });
                    return false;
                }
            });
        }
    };

$(function () {
    userCenter.start();
})
